package 集合0911.Collection;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class 递归 {
    public static void main(String[] args) {
        List<Person> cld = new ArrayList<>();
        Person root = new Person("tom", 23);
        Person s2 = new Person("jerry", 22);
        Person s3 = new Person("alice", 20);
        Person s4 = new Person("Ada", 25);
        s2.setParent(root);
        s3.setParent(root);
        cld.add(s2);
        cld.add(s3);
        root.setChildren(cld);
        List<Person> sun = new ArrayList<>();
        s4.setParent(s2);
        sun.add(s4);
        s2.setChildren(sun);
        out(root);
        System.out.println(pathToRoot(s4));
    }

    public static void out(Person root) {
        if (root == null) return;
        System.out.println(root);
        if (root.getChildren() != null)
            for (Person p : root.getChildren()) {
                out(p);
            }
    }
    public static String pathToRoot(Person p){
        String name=p.getName()+"->";
        if(p.getParent()!=null)
            name+=pathToRoot(p.getParent());
        return name;
    }
}

class Person {
    private String name;
    private Integer age;
    private Person parent;
    private List<Person> children;

    public Person() {
    }

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public List<Person> getChildren() {
        return children;
    }

    public void setChildren(List<Person> children) {
        this.children = children;
    }

    public Person getParent() {
        return parent;
    }

    public void setParent(Person parent) {
        this.parent = parent;
    }

    @Override
    public String toString() {
        return "学生{" +
                "姓名='" + name + '\'' +
                ", 年龄=" + age +
                '}';
    }
}
